﻿Imports DTO

Public Class SachDAO
    Public Shared Function SelectSachLikeName(ByVal s As SachDTO) As DataTable
        Dim sql As String = "select * from SACH where TenSach like '%" & s.TenSach & "%'"
        Return DatabassProvider.ExcuQuery(sql)
    End Function

    Public Shared Function SelectSachLikeMaTheLoai(ByVal s As SachDTO) As DataTable
        Dim sql As String = "select * from SACH where MaTheLoai like '%" & s.MaTheLoai & "%'"
        Return DatabassProvider.ExcuQuery(sql)
    End Function

    Public Shared Function SelectSachLikeTacGia(ByVal s As SachDTO) As DataTable
        Dim sql As String = "select * from SACH where TacGia like '%" & s.TacGia & "%'"
        Return DatabassProvider.ExcuQuery(sql)
    End Function

    Public Shared Function SelectSachAll() As DataTable
        Dim sql As String = "select* from SACH"
        Dim dt As DataTable = DatabassProvider.ExcuQuery(sql)
        Return dt
    End Function

    Public Shared Sub Insert(ByVal s As SachDTO)
        Dim sql As String = "insert into SACH(TenSach,TacGia,MaTheLoai,GiaBan,SoLuongTon) values('" & s.TenSach & "','" & s.TacGia & "'," & s.MaTheLoai + AscW(",") + s.GiaBan & "," & s.SoLuongTon & ")"
        DatabassProvider.ExcuNonQuery(sql)
    End Sub

    Public Shared Function Delete(ByVal s As SachDTO) As Boolean
        Try
            Dim sql As String = "delete from SACH where MaSach= " & s.MaSach & ""
            DatabassProvider.ExcuNonQuery(sql)
            Return True
        Catch
            Return False
        End Try
    End Function

    Public Shared Sub Update(ByVal s As SachDTO)
        Dim sql As String = "update  SACH set TenSach=('" & s.TenSach & "'),TacGia=('" & s.TacGia & "'),MaTheLoai=(" & s.MaTheLoai & "),GiaBan=(" & s.GiaBan & "),SoLuongTon=(" & s.SoLuongTon & ") where MaSach = " & s.MaSach & ""
        DatabassProvider.ExcuNonQuery(sql)
    End Sub

    Public Shared Sub UpdateSoLuongTon(ByVal s As SachDTO)
        Dim sql As String = "update SACH set SoLuongTon=(" & s.SoLuongTon & ") where MaSach = " & s.MaSach & ""
        DatabassProvider.ExcuNonQuery(sql)
    End Sub

    Public Shared Function SelectTLByName(ByVal name As String) As SachDTO
        Dim sql As String = "select * from SACH where TenSach='" & name & "'"
        Dim dt As DataTable = DatabassProvider.ExcuQuery(sql)
        If dt.Rows.Count = 0 Then
            Return Nothing
        Else
            Dim s As New SachDTO()
            s.TenSach = dt.Rows(0).ItemArray(0).ToString()
            Return s
        End If
    End Function

    Public Shared Function SelectSachTheoMa(ByVal ma As Integer) As SachDTO
        Dim sql As String = "select * from SACH where MaSach=" & ma & ""
        Dim dt As DataTable = DatabassProvider.ExcuQuery(sql)
        If dt.Rows.Count = 0 Then
            Return Nothing
        Else
            Dim s As New SachDTO()
            s.MaSach = Integer.Parse(dt.Rows(0).ItemArray(0).ToString())
            Return s
        End If
    End Function

    Public Shared Function GetSachCondition(ByVal SoLuongTon As Integer) As DataTable
        Dim sql As String = "select * from SACH where SoLuongTon < " & SoLuongTon
        Return DatabassProvider.ExcuQuery(sql)
    End Function

    Public Shared Function SelectPNByName(ByVal p As Object) As Object
        Throw New Exception("The method or operation is not implemented.")
    End Function
End Class
